# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Maintainer: Jan de Groot <jgc@archlinux.org>

pkgbase=freetype2
pkgname=(freetype2 freetype2-demos freetype2-docs)
pkgver=2.11.0
pkgrel=4
pkgdesc="Font rasterization library"
url="https://www.freetype.org/"
arch=(x86_64)
license=(GPL)
# adding harfbuzz for improved OpenType features auto-hinting
# introduces a cycle dep to harfbuzz depending on freetype wanted by upstream
depends=(zlib bzip2 sh libpng harfbuzz brotli)
makedepends=(libx11 qt5-base meson)
source=(https://download-mirror.savannah.gnu.org/releases/freetype/freetype-$pkgver.tar.xz{,.sig}
        https://download-mirror.savannah.gnu.org/releases/freetype/freetype-doc-$pkgver.tar.xz{,.sig}
        https://download-mirror.savannah.gnu.org/releases/freetype/ft2demos-$pkgver.tar.xz{,.sig}
        0001-Enable-table-validation-modules.patch
        0002-Enable-subpixel-rendering.patch
        0003-Enable-infinality-subpixel-hinting.patch
        0004-Enable-long-PCF-family-names.patch
        0005-ft2-demos-meson-fixes.patch
        0006-Return_FT_Err_Ok_while_trying_to_render_bitmap.patch
        0007-Restore_quiet_no-op_rendering_of_bitmap_glyphs.patch
        0008-sfnt-Add-API-for-retrieving-a-COLR-v1-ClipBox-table.patch
        0009-src-sfnt-ttcolr.c-tt_face_get_color_glyph_clipbox-Mi.patch
        0010-sfnt-Add-missing-blend-mode-plus-to-COLR-v1.patch
        freetype2.sh)
sha256sums=('8bee39bd3968c4804b70614a0a3ad597299ad0e824bc8aad5ce8aaf48067bde7'
            'SKIP'
            '5b0b5f504f3547ab6123049917b36cfe6944837eaa7baa86ae742a596b5c206c'
            'SKIP'
            'eb0622296c6dfb38fc305c99ebbfb5c770db7e344b94f63042ec03f3db164550'
            'SKIP'
            'e606bdba5c0ee698902886140e4643551ffc8327b1b7d2b0c9129d1f93b1f36c'
            '17ab1609cdbcaba3c5975f5cf1ffd078e067d560f906d5507390d41997e6c468'
            '561d2f2503d180b796f868470612610f6d7fcb34efa0620ecab38fd39002e27a'
            '9c068a984e7b9a27ff9e709839d522b66815ac4e8c7a5bd4ea0224335f7f2ef6'
            '7b1268c229414bc75c50fa826a0f8d391c15568742bca10afd8c12f0c61e5597'
            '2b13b8cc9acc3e56be6b0f8102d648864227bf93637bc956d5052c77c8509782'
            '21a2d243bc6b44d1cdb88ef29af2bd5ceda8d0faaf928bdc2c078a474ddc61f1'
            '13aba5006db13522ff0f3ffc85f81058b6cfcc48671285b1a90ac041a6a51952'
            '09a01b99839fc864db7a2c0748b4703ac5b04897cbd35e0d8969024b4ba89649'
            'fbabfb745db32e1d90f9798e1018da26578f3bce9714dac8e64c15b691e76f69'
            'f7f8e09c44f7552c883846e9a6a1efc50377c4932234e74adc4a8ff750606467')
validpgpkeys=(58E0C111E39F5408C5D3EC76C1A60EACE707FDA5) # Werner Lemberg <wl@gnu.org>

prepare() {
  pushd freetype-$pkgver
  patch -Np1 -i ../0001-Enable-table-validation-modules.patch
  patch -Np1 -i ../0002-Enable-subpixel-rendering.patch
  patch -Np1 -i ../0003-Enable-infinality-subpixel-hinting.patch
  patch -Np1 -i ../0004-Enable-long-PCF-family-names.patch
  # https://gitlab.freedesktop.org/freetype/freetype/-/issues/1076
  patch -Np1 -i ../0006-Return_FT_Err_Ok_while_trying_to_render_bitmap.patch
  patch -Np1 -i ../0007-Restore_quiet_no-op_rendering_of_bitmap_glyphs.patch

  # Add API for retrieving a 'COLR' v1 'ClipBox' table (used by Chromium 94)
  patch -Np1 -i ../0008-sfnt-Add-API-for-retrieving-a-COLR-v1-ClipBox-table.patch
  patch -Np1 -i ../0009-src-sfnt-ttcolr.c-tt_face_get_color_glyph_clipbox-Mi.patch
  patch -Np1 -i ../0010-sfnt-Add-missing-blend-mode-plus-to-COLR-v1.patch

  ln -sr . ../ft2demos-$pkgver/subprojects/freetype2
  popd

  pushd ft2demos-$pkgver
  patch -Np1 -i ../0005-ft2-demos-meson-fixes.patch
  popd
}

build() {
  arch-meson ft2demos-$pkgver build \
    -D freetype2:default_library=shared
  meson compile -C build
}

check() {
  meson test -C build --print-errorlogs
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_freetype2() {
  provides=(libfreetype.so)
  install=freetype2.install
  backup=(etc/profile.d/freetype2.sh)

  meson install -C build --destdir="$pkgdir"
  install -Dt "$pkgdir/etc/profile.d" -m644 freetype2.sh
  install -Dt "$pkgdir/usr/share/aclocal" -m644 \
    freetype-$pkgver/builds/unix/freetype2.m4

  cd "$pkgdir"
  _pick demos usr/bin
  _pick demos usr/share/man/man1
}

package_freetype2-demos() {
  pkgdesc="Freetype tools and demos"
  depends=(freetype2 libx11)
  optdepends=('qt5-base: ftinspect')
  mv demos/* "$pkgdir"
}

package_freetype2-docs() {
  pkgdesc="Freetype documentation"
  depends=(freetype2)

  mkdir -p "${pkgdir}/usr/share/doc"
  cp -r freetype-$pkgver/docs "$pkgdir/usr/share/doc/freetype2"
}

# vim:set sw=2 et:
